package com.crack.smartgit;

import java.io.*;
import java.util.HashMap;

public final class VersionMap {
    public final static String ENTRY = "MAIN";
    public final static String METHOD = "METHOD";
    public final static String PARMTYPELIST = "PARMTYPELIST";


    public final static HashMap<String, String> V18_2_5 = new HashMap<String, String>();

    static {
        V18_2_5.put(ENTRY, "smartgit/alV");
        V18_2_5.put(METHOD, "a");
        V18_2_5.put(PARMTYPELIST, "ajA,MessageDigest");
    }

    public final static HashMap<String, String> V18_2_6 = new HashMap<String, String>();

    static {
        V18_2_6.put(ENTRY, "smartgit/alW");
        V18_2_6.put(METHOD, "a");
        V18_2_6.put(PARMTYPELIST, "ajB,MessageDigest");
    }

    public final static HashMap<String, String> V18_2_7 = new HashMap<String, String>();

    static {
        V18_2_7.put(ENTRY, "smartgit/alZ");
        V18_2_7.put(METHOD, "a");
        V18_2_7.put(PARMTYPELIST, "ajE,MessageDigest");
    }

    public final static HashMap<String, HashMap> Versions = new HashMap<String, HashMap>();

    static {
        Versions.put("18.2.5", V18_2_5);
        Versions.put("18.2.6", V18_2_6);
        Versions.put("18.2.7", V18_2_7);
    }

    private final static String CONFIG = "target.txt";
    public static String CurrentVersion = "18.2.5";

    public static HashMap GetTargetVersion(String version) {
        if (version != null && version.length() > 0) {
            if (Versions.containsKey(version)) {
                return Versions.get(version);
            }
        }
        File f = new File(CONFIG);
        HashMap result = null;
        if (!f.exists()) {
            try {
                LogHelper.Debug("The configuration file does not exist and the configuration file will be automatically created and written to the supported default version 18.2.5");
                result = Versions.get(CurrentVersion);
                f.createNewFile();
                FileWriter fileWriter = new FileWriter(f);
                fileWriter.write(CurrentVersion);
                fileWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
                LogHelper.Error("Write configuration failed", e);
            }
        } else {
            try {
                BufferedReader reader = new BufferedReader(new FileReader(f));
                CurrentVersion = reader.readLine().trim();
                result = Versions.getOrDefault(CurrentVersion, null);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                LogHelper.Error("configuration file not found", e);
            } catch (IOException e) {
                e.printStackTrace();
                LogHelper.Error("Write configuration failed", e);
            }
        }
        return result;
    }
}
